import time

from app import dBSession
from app.Models.Files import FileForBM
from app.Models.Model import BmFile
from app.Vendor.ApiException import fileSaveErr


class FileService:
    """
        日志服务层
    """

    def add(self, project_id, main_file_path="null", label_file_path="null", coord_file_path="null"):
        data = {
            'project_id': project_id,
            'main_file_path': main_file_path,
            'label_file_path': label_file_path,
            'coord_file_path': coord_file_path,
            'update_time': int(time.time())
        }
        try:
            file = BmFile(**data)
            dBSession.add(file)
            dBSession.flush()
            id = file.id
            dBSession.commit()
            return id
        except Exception as e:
            dBSession.rollback()
            return 0

    def update(self, filters, dataType, data):
        flag = FileForBM.update(filters, dataType, data, int(time.time()))
        return flag
